package com.huashi.dealer.core.util;

import com.huashi.dealer.core.exception.ServiceException;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

public final class BizAssertUtil {
    private BizAssertUtil() {}

    // 通用非空断言
    public static <T> T notNull(T obj, int errCode, String msg) {
        if (obj == null) {
            throw new ServiceException(errCode, msg);
        }
        return obj;
    }

    // 金额大于 0 断言
    public static BigDecimal gt0(BigDecimal amount, int errCode, String msg) {
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new ServiceException(errCode, msg);
        }
        return amount;
    }

    // 通用布尔断言
    public static void isTrue(boolean condition, int errCode, String msg) {
        if (!condition) {
            throw new ServiceException(errCode, msg);
        }
    }

    // 字符串非空断言
    public static String notEmpty(String str, int errCode, String msg) {
        if (str == null || str.trim().isEmpty()) {
            throw new ServiceException(errCode, msg);
        }
        return str;
    }

    // 集合非空断言
    public static <T extends Collection<?>> T notEmpty(T collection, int errCode, String msg) {
        if (collection == null || collection.isEmpty()) {
            throw new ServiceException(errCode, msg);
        }
        return collection;
    }

    // Map 非空断言
    public static <K, V> Map<K, V> notEmpty(Map<K, V> map, int errCode, String msg) {
        if (map == null || map.isEmpty()) {
            throw new ServiceException(errCode, msg);
        }
        return map;
    }

    // 链式断言载体
    public static class Chain<T> {
        private final T value;
        private Chain(T value) { this.value = value; }

        public Chain<T> notNull(int errCode, String msg) {
            BizAssertUtil.notNull(value, errCode, msg);
            return this;
        }

        public Chain<T> gt0(int errCode, String msg) {
            if (!(value instanceof BigDecimal)) {
                throw new ServiceException(errCode, "值不是BigDecimal类型");
            }
            BizAssertUtil.gt0((BigDecimal) value, errCode, msg);
            return this;
        }

        public Chain<T> isTrue(Function<T, Boolean> condition, int errCode, String msg) {
            if (!condition.apply(value)) {
                throw new ServiceException(errCode, msg);
            }
            return this;
        }

        public Chain<T> notEmpty(int errCode, String msg) {
            if (value instanceof String) {
                BizAssertUtil.notEmpty((String) value, errCode, msg);
            } else if (value instanceof Collection) {
                BizAssertUtil.notEmpty((Collection<?>) value, errCode, msg);
            } else if (value instanceof Map) {
                BizAssertUtil.notEmpty((Map<?, ?>) value, errCode, msg);
            } else {
                throw new ServiceException(errCode, "不支持的类型: 只能校验String、Collection、Map");
            }
            return this;
        }

        public Chain<T> then(Consumer<T> checker) {
            checker.accept(value);
            return this;
        }

        public T get() {
            return value;
        }
    }

    // 入口：把值包成链式
    public static <T> Chain<T> that(T value) {
        return new Chain<>(value);
    }
} 